<?php
namespace app\home\controller;
use think\Controller;
class Playlog extends Controller{
    public function get(){
		//print_r(cookie('zanpian_playlog')) ;
		$list=model('playlog')->find();
        $this->assign('list',$list);
		$path = config('template.view_path', ROOT_PATH . 'tpl' . DS . 'user' . DS) . $GLOBALS['config']['site']['user_theme'] . DS;
		$this->view->config('view_path', $path);
		return $this->fetch('playlog/get');
    }
	public function ajax(){
		$user = session('user_auth');
		if($user['user_id']){
			$where=[];
			$where['uid'] = $user['user_id'];
			$rs = model('Playlog');
		    $list = $rs->listData($where);//查询所有结果集
			if($list){
				foreach ($list as $key =>$value){
					$data[$key]['id'] = $value['log_id'];
					$data[$key]['vod_name'] = $value['vod_name'];
					$data[$key]['url_name'] = $value['log_urlname'];
					$data[$key]['vod_readurl'] = zanpian_read_url('home/vod/read',$value);
					$data[$key]['vod_palyurl'] = zanpian_play_url($value,array('sid'=>$value['log_sid'],'pid'=>$value['log_pid']));
					if($value['log_pid'] < $value['log_maxnum']){
					   $data[$key]['vod_playnext']=zanpian_play_url($value,array('sid'=>$value['log_sid'],'pid'=>$value['log_pid']+1));
					}
					$data[$key]['log_addtime'] = $value['log_urlname'];
				}
			}
			$result["r"] =  $data;
			$result["code"] = count($list) ;
	    }else{
		   $result["code"] = -1;
		}
	    return json($result);
	}	
	public function list(){
		if(is_login() && ($this->request->isPost() || $this->request->isAjax())){
		$user = session('user_auth');
		if($user['user_id']){
			$where=[];
			$where['uid'] = $user['user_id'];
			$where['order']='log_addtime asc';
			$rs = model('Playlog');
		    $list = $rs->listData($where);//查询所有结果集
			if($list){
				$array = array();
				foreach ($list as $key =>$value){
					$data[$key]['log_id'] = $value['log_id'];
					$data[$key]['log_vid'] = $value['log_vid'];
					$data[$key]['log_sid'] = $value['log_sid'];
					$data[$key]['log_pid'] = $value['log_pid'];
					$data[$key]['log_name'] = $value['vod_name'];
					$data[$key]['log_title'] = $value['log_urlname'];
					$data[$key]['log_maxnum'] = $value['log_maxnum'];
					$data[$key]['log_url'] = zanpian_read_url('home/vod/read',$value);
					$data[$key]['log_palyurl'] = zanpian_play_url($value,array('sid'=>$value['log_sid'],'pid'=>$value['log_pid']));
					if($value['log_pid'] < $value['log_maxnum']){
					   $data[$key]['log_next']=zanpian_play_url($value,array('sid'=>$value['log_sid'],'pid'=>$value['log_pid']+1));
					   $data[$key]['log_isend']=0;
					}else{
					   $data[$key]['log_isend']=1;
					}
					$data[$key]['log_time'] = date("Ynd",$value['log_addtime']);
				}
			}
			$result["data"] =  $data;
			$result["code"] = count($list) ;
	    }else{
		   $result["code"] = -1;
		}
	    return json($result);
		}
	}	
    public function set(){
		if($this->request->isPost()){
            $data['log_vid'] = input('log_vid/d');
			$data['log_sid'] = input('log_sid/d');
			$data['log_pid'] = input('log_pid/d') ;
			$data['log_urlname'] = input('log_urlname/s');
			$data['log_maxnum'] = input('log_maxnum/d');
			return model('playlog')->add($data);
		}
    }
    public function add(){
		if($this->request->isPost()){
            $data['log_vid'] = input('vod_id/d');
			$data['log_sid'] = input('vod_sid/d');
			$data['log_pid'] = input('vod_pid/d') ;
			$data['log_urlname'] = input('url_name/s');
			$data['log_maxnum'] = input('vod_maxnum/d');
			return model('playlog')->add($data);
		}
    }	
    public function clear() {
		if(is_login() && ($this->request->isPost() || $this->request->isAjax())){
			$user = session('user_auth');			
			$result=db('playlog')->where('log_uid',$user['user_id'])->delete();
			if ($result) {
				return ['msg'=>'清空成功','code' => 1];
			} else {
				return ['msg'=>'清空失败','code' => -1];
			}
		}
		
    }
    public function del(){
		if(is_login() && ($this->request->isPost() || $this->request->isAjax())){
			$user = session('user_auth');
            $where['log_id'] = input('log_id/d');
			$where['log_uid'] = $user['user_id'];
			$result=db('playlog')->where($where)->delete();
			if ($result) {
				return ['msg'=>'删除成功','code' => 1];
			} else {
				return ['msg'=>'删除失败','code' => -1];
			}
		}
    }		

}
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
